//
//  ViewController.m
//  05_ScrollView
//
//  Created by dllo on 16/7/20.
//  Copyright © 2016年 yh. All rights reserved.
//

#import "ViewController.h"
// 宏定义的两种命名 (预编译指令)
// #define WIDTH
// #define kWidth
// 宏可以嵌套

#define WIDTH [UIScreen mainScreen].bounds.size.width
#define HEIGHT [UIScreen mainScreen].bounds.size.height


@interface ViewController () <UIScrollViewDelegate>

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT)];
    scrollView.backgroundColor = [UIColor redColor];
    [self.view addSubview:scrollView];
    NSLog(@"改变前%@", NSStringFromCGRect(scrollView.bounds));
    // 设置scrollView可以滚动的范围
    scrollView.contentSize = CGSizeMake(0, 1000);
    UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
    button.frame = CGRectMake(20, 100, WIDTH - 40, 40);
    button.backgroundColor = [UIColor cyanColor];
    [scrollView addSubview:button];
    // contentOffset 偏移量 滚动的位置 不设置contentSize contentOffset也可以改变
    
    // 改变contentOffset 实质上是对scrollView的bounds进行修改
    scrollView.contentOffset = CGPointMake(0, 0);
    NSLog(@"改变后%@", NSStringFromCGRect(scrollView.bounds));

    // 添加滚动区域
    scrollView.contentInset = UIEdgeInsetsMake(50, 50, 50, 50);
    // 锁定垂直或竖直方向 (45度角会失效)
    scrollView.directionalLockEnabled = YES;
    // 回弹效果 默认为yes 当为no时水平和数值方向回弹效果都关闭
//    scrollView.bounces = NO;
    // 当contentsize 的垂直滚动范围为0时 保证垂直方向的回弹效果
    scrollView.alwaysBounceVertical = YES;
    // 当contentsize 的水平滚动范围为0时 保证水平方向的回弹效果
    scrollView.alwaysBounceHorizontal = YES;
    // 按照页来滚动
//    scrollView.pagingEnabled = YES;
    // 空值scrollView的滚动yes为能滚动 no为不能滚动
    scrollView.scrollEnabled = YES;
    // 设置是否显示水平或垂直的滚动条 默认为yes 显示滚动条
    scrollView.showsHorizontalScrollIndicator = NO;
    scrollView.showsVerticalScrollIndicator = YES;
    // 设置滚动条的位置
    scrollView.scrollIndicatorInsets = UIEdgeInsetsMake(200, 0, 0, 0);
    // 设置滚动条的样式
    scrollView.indicatorStyle = UIScrollViewIndicatorStyleDefault;
    
//    scrollView.tracking 用户是否开始拖动 yes 为开始拖动 no 未拖动
//    scrollView.dragging 用户是否正在拖动
//    scrollView.decelerating 是否开始减速
    // 默认yes 双击状态栏 返回最初位置
    scrollView.scrollsToTop = YES;
    // scrollView代理
    scrollView.delegate = self;
    
    scrollView.keyboardDismissMode = UIScrollViewKeyboardDismissModeOnDrag; // 当滑动scrollView键盘消失 回收键盘
    // 测试
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(20, 300, WIDTH - 40, 30)];
    textField.backgroundColor = [UIColor cyanColor];
    [scrollView addSubview:textField];
    // 缩放系数 要实现视图缩放必须实现viewForZoomingInScrollView:(UIScrollView *)scrollView
    scrollView.minimumZoomScale = 0.5;
    scrollView.maximumZoomScale = 2.0;
    
    
}
// scrollView的执行顺序
// 只要滚动就会执行的方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.dragging) { // tracking 用户触摸 dragging scrollView开始滑动
        NSLog(@"正在拖动滚动");
    }
//    if (scrollView.dragging) {
//        <#statements#>
//    }
    else {
        NSLog(@"自动滚动");
    }
    
}
#if 0
// 将要开始拖拽
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    NSLog(@"开始拖拽");

}
// 已经结束拖拽
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate {
    NSLog(@"结束拖拽");

}
// 将要开始进行减速
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    NSLog(@"开始减速");

}
// 结束减速
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    NSLog(@"结束减速");
}
#endif
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
// 与其想要缩放的子视图绑定 scrollView的contentSize会跟随这个子视图变化
- (UIView *)viewForZoomingInScrollView:(UIScrollView *)scrollView {
    return [scrollView.subviews firstObject];
}


@end
